package com.base.sms.handler.aliyun;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.base.sms.config.AliyunConfig;
import com.base.sms.model.enums.SMSHandlerEnum;
import com.base.sms.model.query.NoticeQuery;
import com.base.sms.handler.SendHandler;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.util.Collection;

@Component
public class AliyunSendHandler implements SendHandler {

    private final static Log log = LogFactory.get(AliyunSendHandler.class);

    private static final String OK = "OK";

    private static final String PRODUCT = "Dysmsapi";

    private static final String DOMAIN = "dysmsapi.aliyuncs.com";

    private final AliyunConfig properties;

    private final ObjectMapper objectMapper;

    /**
     * 构造阿里云短信发送处理
     *
     * @param properties   阿里云短信配置
     * @param objectMapper
     */
    @Autowired
    public AliyunSendHandler(AliyunConfig properties, ObjectMapper objectMapper) {
        this.properties = properties;
        this.objectMapper = objectMapper;

        String endPoint = properties.getEndpoint();
        String accessKeyId = properties.getAccessKeyId();
        String accessKeySecret = properties.getAccessKeySecret();

//        IClientProfile profile = DefaultProfile.getProfile(endPoint, accessKeyId, accessKeySecret);
//        DefaultProfile.addEndpoint(endPoint, PRODUCT, DOMAIN);
//
//        acsClient = new DefaultAcsClient(profile);
    }

    @Override
    public boolean send(NoticeQuery noticeQuery, Collection<String> phones) {
        String paramString;
        try {
            paramString = objectMapper.writeValueAsString(noticeQuery.getParams());
        } catch (Exception e) {
            log.debug(e.getMessage(), e);
            return false;
        }

//        SendSmsRequest request = new SendSmsRequest();
//        request.setPhoneNumbers(StringUtils.join(phones, ","));
//        request.setSignName(properties.getSignName());
//        request.setTemplateCode(properties.getTemplates(noticeQuery.getType()));
//        request.setTemplateParam(paramString);

        try {
             //此处sdk有问题
//            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
//
//            if (OK.equals(sendSmsResponse.getCode())) {
//                return true;
//            }
//
//            log.debug("send fail[code={}, message={}]", sendSmsResponse.getCode(), sendSmsResponse.getMessage());
        } catch (Exception e) {
            log.debug(e.getMessage(), e);
        }

        return false;
    }

    @Override
    public String support() {
        return SMSHandlerEnum.ALIYUN.type();
    }

}